<?php

namespace App\Http\Controllers;

use App\Post;
use App\Zan;
use Illuminate\Http\Request;
use App\Comment;

class PostController extends Controller
{
    //文章列表页
    public function index ()
    {
        $posts = Post ::orderBy('created_at', 'desc') -> withCount(['comments','zans']) -> paginate(6);

        return view('post/index', [
            'posts' => $posts
        ]);
//        return view('post/index',compact($posts));
    }

    //详情页
    public function show (Post $post)
    {
        $post ->load('comments');
        return view('post/show', compact('post'));
    }


    //创建文章
    public function create ()
    {

        return view('post/create');
    }


    //创建逻辑
    public function store ()
    {
        //验证
        $this -> validate(request(), [
            'title' => 'required|string|max:100|min:5',
            'content' => 'required|string|min:10',
        ]);
//        $data = request(['title','content']);
        //逻辑
        $user_id = \Auth ::id();
//        $data['title'] = \request('title');
//        $data['content'] = strip_tags(\request('content'));
//        $data['user_id'] = $user_id;
        $data = array_merge(\request([ 'title', 'content' ]), compact('user_id'));

        $postData = Post ::create($data);

        //跳转或渲染
        return redirect('posts');
    }

    //编辑页面
    public function edit (Post $post)
    {
        return view('post/edit', [
            'post' => $post
        ]);
    }


    //编辑逻辑
    public function update (Post $post)
    {
        //验证
        $this -> validate(request(), [
            'title' => 'required|string|max:100|min:5',
            'content' => 'required|string|min:10',
        ]);
        //逻辑
        $this -> authorize('update', $post);//策略
        $data = Post ::where('id', request('id')) -> update(request([ 'title', 'content' ]));
        //跳转或渲染
        return redirect('posts');
    }


    //删除逻辑
    public function delete (Post $post)
    {
        $this -> authorize('delete', $post);//策略
        $post -> delete();
        return redirect('posts');
    }

    //图片上传
    public function imageUpload (Request $request)
    {
        $path = $request -> file('wangEditorH5File') -> storePublicly(md5(time()));
        return assert('storage/' . $path);

    }

    //提交评论
    public function comment (Post $post)
    {
        //验证
        $this -> validate(request(), [
            'content' => 'required|string|min:10',
        ]);
        //逻辑
        $user_id = \Auth ::id();
        $data = array_merge(\request([ 'post_id', 'content' ]), compact('user_id'));

        $commentData = Comment ::create($data);
        //跳转或渲染
        return back();
    }

    //赞
    public function zan (Post $post)
    {
        $param = [
            'user_id' => \Auth ::id(),
            'post_id' => $post -> id,
        ];
        Zan ::firstOrCreate($param);
        return back();
    }

    // 取消赞
    public function unzan (Post $post)
    {
        $post->zan(\Auth ::id())->delete();
        return back();
    }
}
